<?php
namespace app\admin\controller;
use think\Db;
class Bulletin extends SessionCheck
{
    public function index()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
		$pages = pages('bulletin',$where,input('get.page'));
		$data = Db::table('bulletin')->where($where)->whereIn('aid',session('aid'))->limit($pages['limit'])->order('sort desc,id desc')->select();
        return view('',['data' => $data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }
	
	public function edit()
    {
        $id = (int)request()->get('id');
        $sort = request()->get('sort');
        if($sort != ''){
            Db::table('bulletin')->where('id',$id)->update(['sort'=>$sort]);
        }else{
            $error = false;
            if(request()->isPost()){
                $data = request()->post();
                if(empty($data['aid'])){
                    $error = true;
                }else{
                    if($id){
                        Db::table('bulletin')->where('id',$id)->update($data);
                    }else {
                        $id = Db::table('bulletin')->insertGetId($data);
                    }
                }
            }
            $data = Db::table('bulletin')->where(['id' => $id])->find();
            return view('',['data' => $data,'error'=>$error,'menu_list'=>$this->menu_list]);
        }
    }
	
       public function del()
    {
	        $id = input('get.id')?input('get.id'):0;
		$page = input('get.page');
                Db::table('bulletin')->whereIn('aid',session('aid'))->delete($id);
		$this->redirect(url('bulletin/index').'?page='.$page);
	
    }

    public function opinion()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $pages = pages('opinion',$where,input('get.page'));
        $data = Db::table('opinion')->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('time desc')->select();
        foreach ($data as $k => $v){
            $data[$k]['username'] = Db::table('user')->where('id',$v['uid'])->value('name');
        }
        return view('',['data'=>$data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
    }

    public function express_check()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        if(request()->isPost()){
            $data = request()->post();
            $id = $data['id'];
            $uid = $data['uid'];
            unset($data['uid']);
            unset($data['id']);
            $info = Db::table('express_check')->where('id',$id)->whereIn('aid',session('aid'))->find();
            if($data['status'] == 1){
                Db::table('user')->where('id',$uid)->update(['isExpress'=>1,'idcard'=>$info['idcard'],'cut'=>'10','idcardimg'=>$info['idcardimg'],'true_name'=>$info['true_name'],'true_head_img'=>$info['true_head_img'],'express_type'=>$info['express_type'],'bank_card'=>$info['bank_card']]);
                $msg = '恭喜，您的实名认证已通过！';
            }else{
                $msg = '抱歉，您的实名认证被拒绝！';
            }
            Db::table('express_check')->where('id',$id)->whereIn('aid',session('aid'))->update($data);
            push(md5($uid),json_encode(['type'=>1,'message'=>$msg,'parameters'=>'','status'=>2]));
            insert_message($uid,'实名认证',$msg,2);

        }else{
            $pages = pages('express_check',$where,input('get.page'));
            $data = Db::table('express_check')->where($where)->whereIn('aid',session('aid'))->limit($pages['limit'])->order('id desc')->select();
            foreach ($data as $k => $v){
                $data[$k]['username'] = Db::table('user')->where('id',$v['uid'])->value('name');
                $data[$k]['express_type_name'] = Db::table('express_type')->where('id',$v['express_type'])->value('name');
            }
            return view('',['data'=>$data,'pages' => $pages['pages'],'menu_list'=>$this->menu_list]);
        }
    }

    public function other()
    {
        $data = [];
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $tab = request()->get('tab');
        if(!empty($tab)){
            $post = request()->post();
            Db::table('express_config')->whereIn('aid',session('aid'))->where($where)->update($post);
        }
        if(!empty($aid)) {
            $data = Db::table('express_config')->whereIn('aid', session('aid'))->where($where)->find();
        }
        return view('',['data'=>$data,'menu_list'=>$this->menu_list]);
    }

}
